package no.hio.wsprovider.dao;

import static org.junit.Assert.*;


import java.util.List;

import junit.framework.TestCase;

import no.hio.wsprovider.dao.ProductDAO;
import no.hio.wsprovider.dao.ProductDAOSimpleImpl;
import no.hio.wsprovider.vo.Product;

import org.apache.velocity.test.provider.TestProvider;
import org.junit.Before;
import org.junit.Test;

public class ProductDAOSimpleImplTestCase extends TestCase{

	Product dummy = new Product();
	Product dummy2 = new Product();
	
	@Before
	public void setUp() throws Exception {
		dummy.setProductID(3);
		dummy.setTitle("Stereo");
		dummy.setDescription("Hifi from boston");
		dummy.setPrice(300.0);
		
		dummy2.setPrice(40.0);
		dummy2.setProductID(4);
		dummy2.setTitle("Bucket");
		dummy2.setDescription("Swashbucklerbucket");
	}

	@Test
	public void testFindAll() {
		ProductDAO dao = new ProductDAOSimpleImpl();
		List<Product> result = dao.findAll();
		assertEquals("Settet må inneholde 2 produkter.", 2, result.size());
		Product p1 = dao.findAll().get(0);
		p1.setPrice(1000000.0);
		Product p2 = dao.findAll().get(0);
		assertFalse("Objekt må lagres for å registrere endringer.", p1.equals(p2));
	}

	@Test
	public void testFindbyDescription() {
		ProductDAO dao = new ProductDAOSimpleImpl();
		dao.insert(dummy);
		dao.insert(dummy2);
		List<Product> result = dao.findbyDescription(dummy.getDescription());
		assertTrue("Returlisten inneholder ikke dummyverdien", result.contains(dummy));
		assertFalse("Returlisten skal ikke inneholde dummy2!", result.contains(dummy2));
	}

	@Test
	public void testFindByProductID() {
		ProductDAO dao = new ProductDAOSimpleImpl();
		dao.insert(dummy);
		Product find = dao.findByProductID(dummy.getProductID());
		assertTrue("Objektet som ble funnet er ikke det samme som ble lagret",
				dummy.equals(find));
		assertEquals(1, dao.findByProductID(1).getProductID());
	}

	@Test
	public void testFindByTitle() {
		ProductDAO dao = new ProductDAOSimpleImpl();
		dao.insert(dummy);
		dao.insert(dummy2);//not to be found in search
		List<Product> result = dao.findByTitle(dummy.getTitle());
		assertTrue("Returlisten inneholder ikke dummyverdien", result.contains(dummy));
		assertFalse("Resultatlisten skal ikke inneholde dummy2", result.contains(dummy2));
	}

	@Test
	public void testRemove() {
		ProductDAO dao = new ProductDAOSimpleImpl();
		dummy.setProductID(dao.insert(dummy));
		
		assertEquals("Product not insertet properly", dummy, dao.findByProductID(dummy.getProductID()));
		
		dao.remove(dummy);
		assertNull("Should been null", dao.findByProductID(dummy.getProductID()));
		
	}

	@Test
	public void testUpdate() {
		ProductDAO dao = new ProductDAOSimpleImpl();
		dummy.setProductID(dao.insert(dummy));
		
		dummy.setTitle("New Title");
		dao.update(dummy);
		assertEquals("Original tittel er endret", "New Title", dao.findByProductID(dummy.getProductID()).getTitle());
		
	}

}
